/**
 * 初始化系统参数详情对话框
 */
var EntranceGuardRuleInfoDoorSelect = {
    entranceGuardRuleInfoDoorSelectData : {},
    doorTree: null,
    selectedTree: null,
    pre_ids: [],
    ids: []
};

/**
 * 清除数据
 */
EntranceGuardRuleInfoDoorSelect.clearData = function() {
    this.entranceGuardRuleInfoDoorSelectData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EntranceGuardRuleInfoDoorSelect.set = function(key, val) {
    //$("[id='#timeInfo.id']").val('');
    this.entranceGuardRuleInfoDoorSelectData[key] = (typeof val == "undefined") ? $("[id='"+key+"']").val() : val;
    // this.entranceGuardRuleInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
EntranceGuardRuleInfoDoorSelect.get = function(key) {
    return $("[id='"+key+"']").val();
    // return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
EntranceGuardRuleInfoDoorSelect.close = function() {
    parent.layer.close(window.parent.EntranceGuardRule.layerIndex);
}

/**
 * 收集数据
 */
EntranceGuardRuleInfoDoorSelect.collectData = function() {
    this
        .set('id')
        .set('version')
        .set('ruleName')
        .set('remark')
        .set('timeInfo.id')
    ;
}

/**
 * 提交
 */
EntranceGuardRuleInfoDoorSelect.submit = function() {

    this.clearData();
    // this.collectData();

    //提交
    // 删除''的元素
    if(EntranceGuardRuleInfoDoorSelect.ids[0]==''){
        EntranceGuardRuleInfoDoorSelect.ids.shift();
        EntranceGuardRuleInfoDoorSelect.pre_ids.shift();
    }
    if(EntranceGuardRuleInfoDoorSelect.pre_ids.sort().toString() == EntranceGuardRuleInfoDoorSelect.ids.sort().toString()){
        Feng.error("未给门禁权限分配新的门！");
        return false;
    };

    var idsArr = "";
    for (var i = 0; i<EntranceGuardRuleInfoDoorSelect.ids.length; i++) {
        idsArr = (idsArr + EntranceGuardRuleInfoDoorSelect.ids[i]) + (((i + 1)== EntranceGuardRuleInfoDoorSelect.ids.length) ? '':',');
    }
    $('#idsArr').val(idsArr);
    this.entranceGuardRuleInfoDoorSelectData["idsArr"] = idsArr;
    this.entranceGuardRuleInfoDoorSelectData["id"] = $("[id='id']").val();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/door/ENTRANCE/GUARD/RULE/assignRuleToDoor", function(data){
        Feng.success("设置成功!");
        EntranceGuardRuleInfoDoorSelect.close();
    },function(data){
        Feng.error("设置失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.entranceGuardRuleInfoDoorSelectData);
    ajax.start();
}


var setting = {view: {selectedMulti:false,nameIsHTML:true,showTitle:false,dblClickExpand:false},
    data: {simpleData: {enable: true}},
    callback: {onClick: treeOnClick}};



//点击选择项回调
function treeOnClick(event, treeId, treeNode, clickFlag){
    $.fn.zTree.getZTreeObj(treeId).expandNode(treeNode);
    if("doorTree"==treeId){
        //alert(treeNode.id + " | " + ids);
        //alert(typeof ids[0] + " | " +  typeof treeNode.id);
        if($.inArray(String(treeNode.id), EntranceGuardRuleInfoDoorSelect.ids)<0){
            EntranceGuardRuleInfoDoorSelect.selectedTree.addNodes(null, treeNode);
            EntranceGuardRuleInfoDoorSelect.ids.push(String(treeNode.id));
        }
    };
    if("selectedTree"==treeId){
        // if($.inArray(String(treeNode.id), EntranceGuardRuleInfoDoorSelect.pre_ids)<0){
            EntranceGuardRuleInfoDoorSelect.selectedTree.removeNode(treeNode);
            EntranceGuardRuleInfoDoorSelect.ids.splice($.inArray(String(treeNode.id), EntranceGuardRuleInfoDoorSelect.ids), 1);
        // }else{
        //     Feng.error("角色原有成员不能清除！");
        // }
    }
};
$(function() {
   
    EntranceGuardRuleInfoDoorSelect.pre_ids= [];
    EntranceGuardRuleInfoDoorSelect.ids= [];
    //初始化已分配门树
    $.get(Feng.ctxPath + "/door/ENTRANCE/GUARD/RULE/selectDoorForRule?id=" + $("[id='id']").val(), function(doorNodes){
        for (var i = 0; i < doorNodes.length; i++) {
            // doorNodes[i].name = "<font color='red' style='font-weight:bold;'>" + doorNodes[i].id + "-" + doorNodes[i].name + "</font>";
            doorNodes[i].name = "<font color='red' style='font-weight:bold;'>" + doorNodes[i].doorName + "</font>";
            EntranceGuardRuleInfoDoorSelect.pre_ids.push(doorNodes[i].id)
            EntranceGuardRuleInfoDoorSelect.ids.push(doorNodes[i].id)
        }
        EntranceGuardRuleInfoDoorSelect.selectedTree = $.fn.zTree.init($("#selectedTree"), setting, doorNodes);
    });


    //初始化所有门树
    $.get(Feng.ctxPath + "/door/DOOR/INFO/getDoorList?id=" + $("[id='id']").val(), function(doors){
        for (var i = 0; i < doors.length; i++) {
            // rules[i].name = rules[i].id + "-" + rules[i].ruleName;
            doors[i].name = doors[i].doorName;
        }
        EntranceGuardRuleInfoDoorSelect.doorTree = $.fn.zTree.init($("#doorTree"), setting, doors);
    });
});
